package com.itheima.wisdomnext.mapper;

import com.itheima.wisdomnext.entity.Topic;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface TopicMapper {

    // 插入话题
    @Insert("INSERT INTO topics (name, description, created_by, created_at, is_official) " +
            "VALUES (#{name}, #{description}, #{createdBy}, NOW(), #{isOfficial})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertTopic(Topic topic);

    // 获取所有话题
    @Select("SELECT * FROM topics")
    List<Topic> getAllTopics();

    // 根据 ID 获取话题（并关联帖子）
    @Select("SELECT * FROM topics WHERE id = #{id} AND approved = 1")
    Topic getTopicById(@Param("id") Integer id);

    // 修改话题
    @Update("UPDATE topics SET name = #{name}, description = #{description}" +
            "WHERE id = #{id}")
    int updateTopic(Topic topic);

    // 删除话题
    @Delete("DELETE FROM topics WHERE id = #{id}")
    int deleteTopic(@Param("id") Integer id);

    // 🔥 审核话题（修改 approved 状态）
    @Update("UPDATE topics SET approved = 1 WHERE id = #{id}")
    int approveTopic(@Param("id") Integer id);
}
